<template>
  <div class="shuku-bg">
    <div class="shuku-wrapper">
      <!-- 左侧筛选栏 -->
      <aside class="shuku-sidebar">
       
        <!-- 作品分类 -->
        <div class="filter-group">
          <div class="filter-title">作品分类</div>
          <div class="filter-list">
            <span v-for="(item, idx) in categories" :key="item" :class="{active: activeCategory===idx}" @click="selectCategory(idx,item)">{{item}}</span>
          </div>
        </div>
        <!-- 作品字数 -->
        <div class="filter-group">
          <div class="filter-title">作品字数</div>
          <div class="filter-list">
            <span v-for="(item, idx) in wordCounts" :key="item" :class="{active: activeWordCount===idx}" @click="selectWordCount(idx,item)">{{item}}</span>
          </div>
        </div>
        <!-- 更新状态 -->
        <div class="filter-group">
          <div class="filter-title">更新状态</div>
          <div class="filter-list">
            <span v-for="(item, idx) in updateStatus" :key="item" :class="{active: activeUpdateStatus===idx}" @click="selectUpdateStatus(idx,item)">{{item}}</span>
          </div>
        </div>
        <!-- 作品状态 -->
        <!-- <div class="filter-group">
          <div class="filter-title">作品状态</div>
          <div class="filter-list">
            <span v-for="(item, idx) in bookStatus" :key="item" :class="{active: activeBookStatus===idx}" @click="selectBookStatus(idx)">{{item}}</span>
          </div>
        </div> -->
      </aside>
      <!-- 右侧小说列表 -->
      <main class="shuku-main">
        <div class="book-list">
          <div class="book-row" v-for="(row,index) in books" :key="index" @click="To_xiangqing(row.id)">
            <div class="book-card">
              <img :src="row.image" class="book-cover">
              <div class="book-info">
                <div class="book-title" @mouseover="hoverBook=row.id" @mouseleave="hoverBook=null" :class="{hover: hoverBook===row.id}">{{row.content}}</div>
                <div class="book-meta">{{row.author}} · {{row.sort[0]}} · {{row.zishu}}字</div>
                <div class="book-desc" @mouseover="hoverDesc=row.id" @mouseleave="hoverDesc=null" :class="{hover: hoverDesc===row.id}">{{row.contents}}</div>
                <div class="book-update">更新时间：{{row.time}}</div>
              </div>
            </div>
          </div>
        </div>
      </main>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      // 当前性别
      gender: 'male',
      // 分类数据
      categories: [ 
        '全部分类',
        '奇幻',
        '成长',
        '都市情感',
        '社会',
        '言情',
        '奇幻冒险',
        '惊悚',
        '科幻',
        '科幻悬疑',
        '情感',
        '穿越'],
      wordCounts: ['全部', '3万字以上', '1万字以上', '2万字以上', '5万字以上',  '1-3万字'],
      updateStatus: ['全部', '连载', '完结'],
      // bookStatus: ['免费', 'VIP'],
      // 当前选中项索引
      activeCategory: 0,
      activeSubCategory: -1,
      activeWordCount: 0,
      activeUpdateStatus: 0,
      activeBookStatus: 0,
      // 鼠标悬停的小说id
      hoverBook: null,
      hoverDesc: null,
      // 小说数据（示例）
      books: [
        {
          id: 1,
          cover: require('@/assets/image/45458c7b.zcepd8.png'),
          title: '人在末世：红警系统已就位',
          author: '她的胖胖',
          category: '游戏',
          words: '165万',
          desc: '孙宇来到末世，红警系统自带兽潮、异能、科技树……',
          updateTime: '2023-12-12 14:55:44'
        }
      ],
      ZuoPinF:'全部分类',
      zishuF:'全部',
      gengxinF:'全部'
    }
  },
  mounted() {
    this.AJAXshuju(this.ZuoPinF, this.zishuF, this.gengxinF);
  },
  methods: {
    // 请求数据
    AJAXshuju(qb, zs, gx) {
      console.log('请求数据:', qb, zs, gx);
      axios.post('/api/index/sortall', {
        sorttitle:qb,
        wordCount:zs,
        updateStatus:gx
      })
        .then(response => {
          console.log('获取小说数据成功:', response.data.xinxi);
          this.books = response.data.xinxi;
          
        })
        .catch(error => {
          console.error('获取小说数据失败:', error);
        });
    },
    // 切换分类
    selectCategory(idx,item) {
      this.activeCategory = idx
      // TODO: 根据分类切换数据
      this.ZuoPinF = item;
      this.AJAXshuju(this.ZuoPinF, this.zishuF, this.gengxinF);
    },
    // 切换字数
    selectWordCount(idx,item) {
      this.activeWordCount = idx
      // TODO: 根据字数切换数据
      this.zishuF = item;
      this.AJAXshuju(this.ZuoPinF, this.zishuF, this.gengxinF);
    },
    // 切换更新状态
    selectUpdateStatus(idx,item) {
      this.activeUpdateStatus = idx
      // TODO: 根据更新状态切换数据
      this.gengxinF = item;
      this.AJAXshuju(this.ZuoPinF, this.zishuF, this.gengxinF);
    },
    // 切换作品状态
    // selectBookStatus(idx) {
    //   this.activeBookStatus = idx
    //   // TODO: 根据作品状态切换数据
    //   this.Huanyuan = idx === 0 ? '免费' : 'VIP';
    // },
    //跳转到详情页
    To_xiangqing(id){
      console.log('跳转到详情页，小说ID:', id);
      this.$router.push('/BookDetails/'+id);
    },

    //跟新状态
    TypeState(zt){
       switch (zt) {
        case 0:
          this.activeUpdateStatus = 0;
          this.gengxinF = '全部';
          break;
        case 1:
          this.activeUpdateStatus = 1;
          this.gengxinF = '连载中';
          break;
        case 2:
          this.activeUpdateStatus = 2;
          this.gengxinF = '已完结';
          break;
      }
    }
  },
  // 计算属性
  computed: {
    status() {
      return this.$store.state.endState;
    }
  },
  // 监视属性
  watch: {
    status() {
      this.TypeState(this.$store.state.endState);
    }
  }
}
</script>

<style lang="scss" scoped>
.shuku-bg {
  width: 100%;
  min-height: 100vh;
  background: #f7f8fa;
  padding: 40px 0;
}
.shuku-wrapper {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  gap: 32px;
}
.shuku-sidebar {
  width: 220px;
  height: 400px;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  padding: 32px 0 32px 18px;
  display: flex;
  flex-direction: column;
  .gender-tabs {
    display: flex;
    justify-content: flex-start;
    gap: 24px;
    margin-bottom: 24px;
    span {
      font-size: 18px;
      color: #444;
      cursor: pointer;
      padding-bottom: 4px;
      border-bottom: 2px solid transparent;
      transition: color 0.2s, border-color 0.2s;
      &.active {
        color: #e94f4f;
        font-weight: bold;
        border-bottom: 2px solid #e94f4f;
      }
    }
  }
  .filter-group {
    margin-bottom: 18px;
    .filter-title {
      font-size: 15px;
      color: #888;
      margin-bottom: 8px;
    }
    .filter-list {
      display: flex;
      flex-wrap: wrap;
      gap: 8px 10px;
      padding: 0 0 0 0px;
      span {
        font-size: 14px;
        color: #444;
        padding: 4px 12px;
        border-radius: 12px;
        cursor: pointer;
        background: #f7f8fa;
        transition: background 0.2s, color 0.2s;
        &.active {
          background: #ffeaea;
          color: #e94f4f;
          font-weight: bold;
        }
        &:hover {
          background: #ffeaea;
          color: #e94f4f;
        }
      }
      &.sub {
        margin-top: 4px;
      }
    }
  }
}
.shuku-main {
  flex: 1;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  padding: 30px 30px;
  min-width: 0;
  .book-list {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    
    .book-row {
      width: 48.5%;
      margin-bottom: 20px;
      .book-card {
        display: flex;
        background: #fff;
        border-radius: 10px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.03);
        padding: 18px 18px 18px 18px;
        transition: box-shadow 0.2s;
        cursor: pointer;
        &:hover {
          box-shadow: 0 4px 16px rgba(233,79,79,0.08);
        }
        .book-cover {
          width: 90px;
          height: 120px;
          object-fit: cover;
          border-radius: 6px;
          margin-right: 18px;
          box-shadow: 0 2px 8px rgb(95, 95, 95);
          background: #f2f2f2;
        }
        .book-info {
          flex: 1;
          min-width: 0;
          display: flex;
          flex-direction: column;
          .book-title {
            font-size: 18px;
            font-weight: bold;
            color: #222;
            margin-bottom: 4px;
            /* 溢出隐藏 */
            overflow: hidden;
            /* 不让文本换行 除非遇到br标签 */
            white-space:nowrap;
            /* 溢出文字用...代替 */
            text-overflow: ellipsis;
            cursor: pointer;
            transition: color 0.2s;
            &.hover {
              color: #e94f4f;
            }
          }
          .book-meta {
            font-size: 13px;
            color: #888;
            margin-bottom: 6px;
          }
          .book-desc {
            font-size: 14px;
            color: #222;
            margin-bottom: 6px;
            line-height: 1.5;
            cursor: pointer;
            transition: color 0.2s;
            max-width: 400px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            &.hover {
              color: #e94f4f;
            }
          }
          .book-update {
            font-size: 13px;
            color: #bbb;
          }
        }
      }
    }
  }
}
@media (max-width: 900px) {
  .shuku-wrapper {
    flex-direction: column;
    gap: 0;
  }
  .shuku-sidebar {
    width: 100%;

    flex-direction: row;
    padding: 0;
    border-radius: 14px 14px 0 0;
    .filter-group {
      margin-bottom: 0;
      .filter-list {
        padding-left: 12px;
      }
    }
  }
  .shuku-main {
    padding: 24px 10px 24px 10px;
  }
  .book-row {
    flex-direction: column !important;
    gap: 16px !important;
    .book-card {
      width: 100% !important;
    }
  }
}
</style>
